package edu.osu.cse.c651.cryptochess;

import java.util.ArrayList;

/**
 * The Bishop
 * 
 * @author Team Pawned
 * 
 */
public class Bishop implements Piece {

	/**
	 * The color of this Bishop
	 */
	private Color color;

	/**
	 * The number of times this piece has been moved
	 */
	private int moveCount;

	/**
	 * Constructor
	 * 
	 * @param color
	 *            the color of this Bishop (Black or White)
	 */
	public Bishop(Color color) {
		this.color = color;
		moveCount = 0;
	}

	/**
	 * 
	 */
	public boolean legalMove(int x1, int y1, int x2, int y2) {
		if ((x1 == x2) || (y1 == y2)) {
			return false;
		}
		if (Math.abs(x1 - x2) == Math.abs(y1 - y2)) {
			return true;
		}
		return false;
	}

	/**
	 * 
	 */
	public Color color() {
		return color;
	}

	/**
	 * 
	 */
	public PieceName name() {
		return PieceName.Bishop;
	}

	/**
	 * 
	 */
	public void movesPlusOne() {
		moveCount += 1;
	}

	/**
	 * 
	 */
	public int moveCount() {
		return moveCount;
	}

	/**
	 * 
	 */
	public ArrayList<Coordinate> possibleMoves(ChessBoard board, int x, int y) {
		ArrayList<Coordinate> moves = new ArrayList<Coordinate>();

		// Loops in all four possible directions until it can go no further
		int i = x + 1;
		int j = y + 1;
		while (i < 8 && j < 8) {
			// if no piece is there, its a possible move
			if (board.getPiece(i, j) == null) {
				Coordinate move = new Coordinate(i, j);
				moves.add(move);
				i++;
				j++;
			}
			// if its an enemy piece, then it can move here, but no further in
			// this direction
			else if (board.getPiece(i, j).color() != this.color()) {
				Coordinate move = new Coordinate(i, j);
				moves.add(move);
				break;
			}
			// otherwise, its a friendly piece and you can't move here
			else
				break;
		}

		i = x + 1;
		j = y - 1;
		while (i < 8 && j >= 0) {
			// if no piece is there, its a possible move
			if (board.getPiece(i, j) == null) {
				Coordinate move = new Coordinate(i, j);
				moves.add(move);
				i++;
				j--;
			}
			// if its an enemy piece, then it can move here, but no further in
			// this direction
			else if (board.getPiece(i, j).color() != this.color()) {
				Coordinate move = new Coordinate(i, j);
				moves.add(move);
				break;
			}
			// otherwise, its a friendly piece and you can't move here
			else
				break;
		}

		i = x - 1;
		j = y + 1;
		while (i >= 0 && j < 8) {
			// if no piece is there, its a possible move
			if (board.getPiece(i, j) == null) {
				Coordinate move = new Coordinate(i, j);
				moves.add(move);
				i--;
				j++;
			}
			// if its an enemy piece, then it can move here, but no further in
			// this direction
			else if (board.getPiece(i, j).color() != this.color()) {
				Coordinate move = new Coordinate(i, j);
				moves.add(move);
				break;
			}
			// otherwise, its a friendly piece and you can't move here
			else
				break;
		}

		i = x - 1;
		j = y - 1;
		while (i >= 0 && j >= 0) {
			// if no piece is there, its a possible move
			if (board.getPiece(i, j) == null) {
				Coordinate move = new Coordinate(i, j);
				moves.add(move);
				i--;
				j--;
			}
			// if its an enemy piece, then it can move here, but no further in
			// this direction
			else if (board.getPiece(i, j).color() != this.color()) {
				Coordinate move = new Coordinate(i, j);
				moves.add(move);
				break;
			}
			// otherwise, its a friendly piece and you can't move here
			else
				break;
		}
		return moves;
	}

}
